package com.android.lovingpet.adopt.adoption.publish.view;

import android.annotation.SuppressLint;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.lovingpet.R;
import com.android.lovingpet.adopt.adoption.AdoptionPresenter;
import com.android.lovingpet.adopt.adoption.IAdoptionContract;
import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.bean.Pet;
import com.android.lovingpet.util.MyDatePickDialog;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;

import org.greenrobot.eventbus.EventBus;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by ikata on 2018/4/26.
 */

public class AdoptionInfoFragment extends BaseFragment<IAdoptionContract.View, AdoptionPresenter> implements IAdoptionContract.View, View.OnClickListener {


    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tb_next)
    Toolbar tbNext;
    public Pet adoptablePet = null;
    Unbinder unbinder;
    @BindView(R.id.tv_birthday)
    TextView tvBirthday;
    @BindView(R.id.sp_source)
    Spinner spSource;
    @BindView(R.id.et_description)
    EditText etDescription;
    @BindView(R.id.sp_sterilize)
    Spinner spSterilize;
    @BindView(R.id.sp_immune)
    Spinner spImmune;
    @BindView(R.id.sp_sex)
    Spinner spSex;
    @BindView(R.id.sp_type)
    Spinner spType;

    @Override
    protected int setContentView() {
        return R.layout.fragment_adoption_info;
    }

    @Override
    protected void onCreateView(View view) {
        unbinder = ButterKnife.bind(this, view);
        setTitle(view, "养宠");
    }

    @Override
    public AdoptionPresenter createPresenter() {
        return new AdoptionPresenter();
    }

    @Override
    public IAdoptionContract.View createView() {
        return this;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tb_next,R.id.tv_birthday})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tb_next:
                String name = etName.getText().toString();
                String birthday = tvBirthday.getText().toString();
                String description = etDescription.getText().toString();
                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(birthday) && !TextUtils.isEmpty(description)) {
                    adoptablePet = new Pet();
                    adoptablePet.setName(name);
                    adoptablePet.setType((spType.getSelectedItem().toString()));
                    adoptablePet.setBirthday(birthday);
                    adoptablePet.setSterilize(spSterilize.getSelectedItemPosition());
                    adoptablePet.setSource(spSource.getSelectedItemPosition());
                    adoptablePet.setDescription(description);
                    adoptablePet.setImmune(spImmune.getSelectedItemPosition());
                    adoptablePet.setGender(String.valueOf(spSex.getSelectedItemPosition()));
                    next();
                } else {
                    Toast.makeText(getContext(), "请将资料填写完整!", Toast.LENGTH_LONG).show();
                }
                break;
            default:
                break;
            case R.id.tv_birthday:
                MyDatePickDialog.Builder builder=new MyDatePickDialog.Builder(getContext());
                builder.setTitle("选择宠物的出生日期")
                        .setType(DateType.TYPE_YMD)
                        .setMessageFormat("yyyy-MM-dd")
                        .setYearLimit(15)
                        .setOnSureLisener(date -> {
                            @SuppressLint("SimpleDateFormat") DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
                            tvBirthday.setText(dateFormat.format(date));
                        }).build().show();
                break;

        }
    }

    @Override
    public void next() {
        getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.adoption_content, new AdoptionVideoFragment(), "demand").commit();
        EventBus.getDefault().postSticky(adoptablePet);
    }

    @Override
    public void addPet(Pet pet) {

    }

}
